#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"


# APP_INSTALL_NAME="liveportrait"
APP_TARGET_DIR_NAME="LivePortrait"

export CURRENT_APP_PATH="$LMD_APPS_DIR/$APP_TARGET_DIR_NAME"
echo "App Dir: ${CURRENT_APP_PATH}"

cd "$CURRENT_APP_PATH"
conda --version

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

export GRADIO_TEMP_DIR="$CURRENT_APP_PATH/temp"
CONDA_PREFIX="./venv"

if [ -n "$GRADIO_SERVER_PORT" ]; then
    START_PARAM="--server_port $GRADIO_SERVER_PORT"
fi

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    conda run -p $CONDA_PREFIX --no-capture-output PYTORCH_ENABLE_MPS_FALLBACK=1 python app.py $START_PARAM
elif current_os_is_windows; then
    conda run -p $CONDA_PREFIX --no-capture-output python app.py $START_PARAM
fi

